/*
 * Please leave this Copyright notice in your code if you use it
 * Written by Decebal Mihailescu [http://www.codeproject.com/script/articles/list_articles.asp?userid=634640]
 */
namespace TextExtractors
{
    using System;
    using System.IO;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.ComponentModel.Composition;
    using CommonUtils;

    [Export(typeof(ITextImport))]
    //[ExportMetadata("SpecialFormat", false)]
    [ExportMetadata("Description", "Html files")]
    [ExportMetadata("FileExtension", "*.Html; *.htm")]
    [ExportMetadata("ContextMenuText", "&Translate Html File")]
    public class HtmlFileReader : BaseTextExtractor, ITextImport
    {
        private readonly Regex _newLinesRegex = new Regex(@"[\r\n]{3,}", RegexOptions.Compiled);
        private readonly Regex _scriptRegex = new Regex(@"<script[^>]*>(.|\n)*?</script[^>]*>", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.IgnoreCase);
        private readonly Regex _tagsOutRegex = new Regex(@"(<[^>]*>)|(&\w+;)", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.IgnoreCase);
        private const string SCRIPT_TAG_END = "</script>";
        private const string SCRIPT_TAG_START = "<script";

        #region ITextImport Members

        public string GetAllTextFromFile(ref string fileName,out Encoding encoding)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentException(null, "fileName");
            }
            if (!File.Exists(fileName))
            {
                throw new ArgumentException("File '" + fileName + "' is not found");
            }
            string input = string.Empty;
            try
            {
                var arg = new ProgressEventArgs();
                arg.Description = "Opening Html file";
                arg.Progress = 0;
                if (ProgressChanged != null && ProgressChanged.GetInvocationList().Length > 0)
                    ProgressChanged(this, arg);
                using (StreamReader reader = new StreamReader(fileName, true))//.GetEncoding(0x4e3)))
                {
                    input = reader.ReadToEnd();
                    encoding = reader.CurrentEncoding;
                    input = this._scriptRegex.Replace(input, "");
                    input = this._tagsOutRegex.Replace(input, "");
                    return this._newLinesRegex.Replace(input, Environment.NewLine);
                }
            }
            catch (Exception exception)
            {
                throw new Exception("Error during text extraction from HTML-file: " + fileName + Environment.NewLine + exception.Message);
            }
            //return input;
        }
        //public string FileDescription
        //{
        //    get { return "Html files"; }
        //}

        //public string FileExtension
        //{
        //    get { return "*.Html; *.htm" ; }
        //}

        public event EventHandler ProgressChanged;
        #endregion
    }
}

